package com.emily.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.ShardingGroup;
import com.emily.test.service.ShardingGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "sg")
@Api(value = "ShardingGroupApi", description = "ShardingGroup接口类", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class ShardingGroupController {

    private Logger logger = LoggerFactory.getLogger(ShardingGroupController.class);
    @Autowired
    private ShardingGroupService shardingGroupService;


    @ApiOperation(value="获取ShardingGroup", notes="获取ShardingGroup")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ShardingGroupID", required = true)
    })
    @PostMapping(value = "selectShardingGroupById")
    public ResponseResult selectShardingGroupById(@RequestBody JSONObject jsonObject){
        int id = jsonObject.getInteger("id");
        try {
            ShardingGroup shardingGroup = shardingGroupService.selectShardingGroupById(id);
            logger.info("获取ShardingGroup={}",shardingGroup);
            return ResponseResult.success(shardingGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }
}
